//
//  WriteMoodViewController.m
//  SunXi
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "WriteMoodViewController.h"

@interface WriteMoodViewController ()<UITextViewDelegate>
@property(nonatomic, retain) UITextView *textView;
@property(nonatomic, retain) UITextView *titView;
@property(nonatomic, retain) UIButton *button;
@property(nonatomic, retain) CoreDataManager *manager;

@end

@implementation WriteMoodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSString *str = @"心情:";
    UIImageView *imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"drawBackPic.jpg"]];
    [self.view addSubview:imageview];
    [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(SV).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(WIDTH  / 3, HEIGHT / 4 * 3, WIDTH / 3, HEIGHT / 20);
    [self.button setTitle:@"确定" forState:UIControlStateNormal];
    [SV addSubview:self.button];
    self.button.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.5];
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.titView = [[UITextView alloc] initWithFrame:CGRectMake(WIDTH / 10, HEIGHT / 15, WIDTH / 10 * 8 , HEIGHT / 15)];
    self.titView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.5];
    self.textView.textAlignment = NSTextAlignmentCenter;
    [SV addSubview:self.titView];
    self.titView.font = [UIFont systemFontOfSize:24];
    self.titView.delegate = self;
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(WIDTH / 20, (HEIGHT - 64) / 5, WIDTH / 10 * 9, (HEIGHT) / 5 * 2)];
    self.textView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.5];
    self.textView.textAlignment = NSTextAlignmentLeft;
    [SV addSubview:self.textView];
    //可编辑
    self.textView.editable = YES;
    self.textView.delegate = self;
    self.textView.text = str;
    self.textView.font = [UIFont systemFontOfSize:18];

    SV.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [SV addGestureRecognizer:tap];
    
    self.manager = [CoreDataManager defaults];
    
    NSLog(@"++==========%@", [self.manager applicationDocumentsDirectory]);
    
}
- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    [self.textView resignFirstResponder];
    [self.titView resignFirstResponder];
    
}


//MARK:button的点击方法
- (void)buttonAction:(UIButton *)button {    
    
    //添加
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"WriteMood" inManagedObjectContext:self.manager.managedObjectContext];
    // 创建实体
    
    WriteMood *mood = [[WriteMood alloc]initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    
    mood.title = self.titView.text;
    mood.label = self.textView.text;
    
    [self.manager saveContext];
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
